package com.sskj.gocoin.v.activity;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.sskj.common.rxbus2.RxBus;
import com.sskj.common.util.ToastUtil;
import com.sskj.gocoin.R;
import com.sskj.gocoin.R2;
import com.sskj.gocoin.common.App;
import com.sskj.gocoin.common.widget.ToolBarLayout;
import com.sskj.gocoin.m.bean.ImgHighBean;
import com.sskj.gocoin.p.activity.VerifyGuidePresenter;
import com.sskj.gocoin.utils.ClickUtil;
import com.sskj.gocoin.v.base.BaseActivity;
import com.sskj.lib.RxBusCode;
import com.tbruyelle.rxpermissions2.RxPermissions;

import java.io.File;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class VerifyGuideActivity extends BaseActivity<VerifyGuidePresenter> {
    @BindView(R2.id.toolBar)
    ToolBarLayout toolBar;

    private final int SELECT_HAND = 1000;
    private final int SELECT_FRONT = 1001;
    private final int SELECT_BACK = 1002;
    @BindView(R2.id.title)
    TextView title;
    @BindView(R2.id.verify_photo_a)
    ImageView verifyPhotoA;
    @BindView(R2.id.rlZm)
    RelativeLayout rlZm;
    @BindView(R2.id.verify_photo_b)
    ImageView verifyPhotoB;
    @BindView(R2.id.rlFm)
    RelativeLayout rlFm;
    @BindView(R2.id.verify_photo_sc)
    ImageView verifyPhotoSc;
    @BindView(R2.id.rlSc)
    RelativeLayout rlSc;
    @BindView(R2.id.verify_submit)
    Button verifySubmit;

    private String imgFirst;
    private String imgSecond;
    private String imgThrid;

    private int position = 1;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_verify_guide;
    }

    @Override
    public VerifyGuidePresenter getPresenter() {
        return new VerifyGuidePresenter();
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, VerifyGuideActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        toolBar.setLeftButtonOnClickLinster(v -> {
            finish();
        });

        ClickUtil.click(rlZm, () -> selectImage(SELECT_FRONT));
        ClickUtil.click(rlFm, () -> selectImage(SELECT_BACK));
        ClickUtil.click(rlSc, () -> selectImage(SELECT_HAND));
        ClickUtil.click(verifySubmit, new ClickUtil.Click() {
            @Override
            public void click() {
                if (TextUtils.isEmpty(imgFirst)) {
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.fabimineXmlactivity_verify_ident22));
                    return;
                }
                if (TextUtils.isEmpty(imgSecond)) {
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.fabimineXmlactivity_verify_ident42));
                    return;
                }
                if (TextUtils.isEmpty(imgThrid)) {
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.fabimineXmlactivity_verify_ident50));
                    return;
                }
                mPresenter.submitVerifyEnd(new File(imgFirst), new File(imgSecond), new File(imgThrid));
            }
        });
    }

    @Override
    protected void onViewBinding() {
        toolBar.setTitle(App.INSTANCE.getString(R.string.mine_verify_2));
    }

    @SuppressLint("CheckResult")
    private void selectImage(int requestCode) {
        new RxPermissions(this)
                .request(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA)
                .subscribe(granted -> {
                    if (granted) {
                        PictureSelector.create(this)
                                .openGallery(PictureMimeType.ofImage())
                                .previewImage(true)
                                .isCamera(true)
                                .imageFormat(PictureMimeType.JPEG)
                                .maxSelectNum(1)
                                .compress(true)
                                .forResult(requestCode);
                    }
                });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if (resultCode == RESULT_OK) {
            List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
            if (selectList != null && selectList.size() > 0) {
                LocalMedia media = selectList.get(0);
                String url;
                if (media.isCompressed()) {
                    url = media.getCompressPath();
                } else {
                    url = media.getPath();
                }
                switch (requestCode) {
                    case SELECT_FRONT:
                        position = 1;
                        imgFirst=url;
                        Glide.with(this).load(url).into(verifyPhotoA);
                        break;
                    case SELECT_BACK:
                        position = 2;
                        imgSecond=url;
                        Glide.with(this).load(url).into(verifyPhotoB);
                        break;
                    case SELECT_HAND:
                        position = 3;
                        imgThrid=url;
                        Glide.with(this).load(url).into(verifyPhotoSc);
                        break;
                }
             //   mPresenter.submitVerify(new File(url), position);
            }
        }

    }

    public void putList(ImgHighBean bean, int position) {
        switch (position) {
            case 1:
                imgFirst = bean.getUrl();
                break;
            case 2:
                imgSecond = bean.getUrl();
                break;
            case 3:
                imgThrid = bean.getUrl();
                break;
        }
    }

    public void onSubmitVerifyResponseSuccess() {
        RxBus.getDefault().send(RxBusCode.VERIFY);
        finish();
    }

    public void uploadFail(int position) {
        switch (position) {
            case 1:
                Glide.with(this).load(getResources().getDrawable(R.mipmap.zhengmian)).into(verifyPhotoA);
                break;
            case 2:
                Glide.with(this).load(getResources().getDrawable(R.mipmap.beimian)).into(verifyPhotoB);
                break;
            case 3:
                Glide.with(this).load(getResources().getDrawable(R.mipmap.shouchi)).into(verifyPhotoSc);
                break;
        }
    }

}
